# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import unittest
from AdventureMoney.core import Utils

class TestCase(unittest.TestCase):
	
	stringsA = ["one", "two", "three", "four"]
	stringsB = ["OnE", "TWO", "THREe", "FouR"]
	stringsC = ["Laa", "sTT", "Jh", "Agf"]
	
	goodDateList = ["2007-04-11", "0001-01-01", "9999-12-31"]
	badDateList = [
		"", "--", "\n", " ", "0-0-0", "2008-13-01",
		"3-54-11", "3007-02-30",
	]
	
	def setUp(self):
		pass
		
	def testStrCmp(self):
		for i, j in zip(self.stringsA, self.stringsB):
			k = i.swapcase()
			m = j.swapcase()
			self.assertEqual(Utils.strCmp(i, j), 0)
			self.assertEqual(Utils.strCmp(k, j), 0)
			self.assertEqual(Utils.strCmp(m, i), 0)
			self.assertEqual(Utils.strCmp(k, m), 0)
			
		for i, j in zip(self.stringsC, self.stringsA):
			k = i.swapcase()
			m = j.swapcase()
			self.assertEqual(Utils.strCmp(i, j), -1)
			self.assertEqual(Utils.strCmp(k, j), -1)
			self.assertEqual(Utils.strCmp(i, m), -1)
			self.assertEqual(Utils.strCmp(k, m), -1)
			
			self.assertEqual(Utils.strCmp(j, i), 1)
			self.assertEqual(Utils.strCmp(j, k), 1)
			self.assertEqual(Utils.strCmp(m, i), 1)
			self.assertEqual(Utils.strCmp(m, k), 1)
			
		self.assertNotEqual(Utils.strCmp("", 3), 0)
	
	def testStrEq(self):
		for i, j in zip(self.stringsA, self.stringsB):
			k = i.swapcase()
			m = j.swapcase()
			self.assert_(Utils.strEq(i, j), 0)
			self.assert_(Utils.strEq(k, j), 0)
			self.assert_(Utils.strEq(k, i), 0)
			self.assert_(Utils.strEq(k, m), 0)
			
		self.failIf(Utils.strEq("", 9))
		
	def testValueDateStr(self):
		for i in self.goodDateList:
			self.failUnless(Utils.isValidDateStr(i), i)
		for i in self.badDateList:
			self.failIf(Utils.isValidDateStr(i), i)
			
	def testDateFromStr(self):
		for i in self.goodDateList:
			try:
				date = Utils.dateFromStr(i)
				self.assertEqual(str(date), i)
				string = Utils.getDateStr(date.year, date.month, date.day)
				self.assertEqual(string, i)
				dateCopy = Utils.dateFromStr(string)
				self.assertEqual(date, dateCopy)
			except Exception, e:
				self.fail(repr(e))
		
	def testTodayDate(self):
		ret = Utils.getTodayDateString()
		self.assert_(Utils.isValidDateStr(ret))
	
	def tearDown(self):
		pass
